<template>
    <el-container class="home_container">
        <!-- 头部区域 -->
        <el-header>
          <div class="header_left">
              <img src="../assets/heima.png" alt="" srcset="">
              <span class="title">电商后台管理系统</span>
          </div>
          <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <!-- 页面主体区域 -->
        <el-container>
            <!-- 侧边栏 -->
            <!-- 通过三元表达式判断侧边栏的大小  :width="isCollapse?'64px':'200px'" -->
            <el-aside :width="isCollapse?'64px':'200px'">
                <div class="toggle_button" @click="toggleCollapse">|||</div>
                <!-- 侧边栏菜单区域 -->
                <el-menu
                    router
                    :default-active="activePath"
                    :collapse-transition="false"
                    :collapse="isCollapse"            
                    unique-opened	
                    background-color="#313743"
                    text-color="#fff"
                    active-text-color="#3d9bff">
                    <!-- 一级菜单 -->
                    <!-- :index="item.id + ''" :index 需要绑定不同的值，且必须为字符全 用以区分不同项 -->
                    <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
                        <!-- 一级菜单的模板区域 -->
                        <template slot="title">
                            <!-- 图标 -->
                            <i :class="iconsObj[item.id]"></i>
                            <!-- 文本 -->
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item @click="saveNavstate('/'+ items.path)" :index="'/'+ items.path" v-for="items in item.children" :key="items.id">
                            <template slot="title">
                                <!-- 图标 -->
                                <i class="el-icon-menu"></i>
                                <!-- 文本 -->
                                <span>{{items.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                    </el-menu>
            </el-aside>
            <!-- 右侧内容主体 -->
            <el-main>
                <!-- 路由占位符 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    data(){
        return{
            menulist:[],// 左侧菜单数据
            iconsObj:{
                '125':'iconfont icon-user',
                '103':'iconfont icon-tijikongjian',
                '101':'iconfont icon-shangpin',
                '102':'iconfont icon-danju',
                '145':'iconfont icon-baobiao',
            },
            isCollapse:false,//菜单是否折叠 false 不折叠  true 折叠
            activePath:'', //被激活的链接地址
        }
    },
    created(){
        this.getMenuList();
        this.activePath = window.sessionStorage.getItem('activePath');
    },
    methods:{
        logout(){
            // 清除存储的数据
            window.sessionStorage.clear();
            //导航重定向
            this.$router.push('/login'); //编程式导航对象
        },
        // 获取所有的菜单
        async getMenuList(){
            const {data:res} = await this.$http.get('menus');
            if(res.meta.status != 200){
                return;
                this.$message.error(res.meta.msg);
            }else{
                this.menulist = res.data;
                this.$message.success(res.meta.msg);
                
            }
        },
        //点击按钮，切换菜单的折叠与展开
        toggleCollapse(){
            this.isCollapse = !this.isCollapse;
        },
        //保存链接的激活状态
        saveNavstate(activePath){
            this.activePath = activePath;
            // 将其点击的选项的值，保存起来
            window.sessionStorage.setItem('activePath',activePath);

        }
    }
}
</script>

<style scoped>
/* 在 element-ui 中 组件名 即 类名 */

.el-header{
    background-color: #363d40;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
}
.el-header .title{
    color: #fff;
    font-size: 25px;
    letter-spacing: 2px;
    margin-left: 13px;
}
.header_left{
    display: flex;
    align-items: center;
}
.home_container{
    height: 100%;
}
.el-aside{
    background-color: #313743;
}
.el-main{
    background-color: #e9edf1;
}
.iconfont{
    margin-right: 10px;
}
.el-menu{
    border: 0;
}
.toggle_button{
    background-color: #475163;
    color: #fff;
    text-align: center;
    font-size: 10px;
    line-height: 24px;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>